import { action, computed, makeObservable, observable } from 'mobx'

class AppleStore {
  constructor() {
    makeObservable(this)
  }
  @observable apples = [
    {
      id: 0,
      weight: 233,
      isEaten: false,
    },
    {
      id: 1,
      weight: 235,
      isEaten: false,
    },
    {
      id: 2,
      weight: 256,
      isEaten: false,
    },
  ]

  @action.bound eatApple(apple){
    apple.isEaten = true
    // this.apples.forEach(item => {
    //   if(item.id === apple.id) {
    //     this.apples[index].isEaten = true
    //   }
    // })
  }

  @computed get appleStatus() {
    const appleStatus = {
      num: 0,
      weight: 0,
      eatWeight: 0,
      eatNum: 0
    }
    this.apples.forEach(apple => {
      console.log(apple, 'apple')
      if(apple.isEaten){
        appleStatus.eatWeight += apple.weight
        appleStatus.eatNum ++
      }else{
        appleStatus.num++
        appleStatus.weight += apple.weight
      }
    })
    return appleStatus
  }
}

export default AppleStore
